package com.asp.alumnisystem.ui;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.asp.alumnisystem.adapter.ForumListAdapter;
import com.asp.alumnisystem.app.App;
import com.asp.alumnisystem.base.BaseActivity;
import com.asp.alumnisystem.base.BaseBean;
import com.asp.alumnisystem.base.BasePresenter;
import com.asp.alumnisystem.base.IView;
import com.asp.alumnisystem.base.IViewAdapter;
import com.asp.alumnisystem.model.ForumModel;
import com.asp.alumnisystem.model.PageModel;
import com.asp.alumnisystem.model.UserModel;
import com.asp.alumnisystem.presenter.ForumListPresenter;
import com.asp.alumnisystem.presenter.OptFriendPresenter;
import com.asp.alumnisystem.utils.ListUtils;
import com.asp.alumnisystem.utils.ToastUtil;
import com.asp.alumnisystem.view.CustomRecyclerView;
import com.asp.alumnisystem.view.TitleBar;

import java.util.List;

/**
 * 个人中心界面
 */
public class PersonalActivity extends BaseActivity implements IView<List<ForumModel>>, CustomRecyclerView.RecyclerListener, TitleBar.OnTitleBarClickListener{

    private CustomRecyclerView customRecyclerView;
    private TitleBar titleBar;
    private UserModel userModel;
    private boolean isFriend;

    private ForumListPresenter forumListPresenter;
    private OptFriendPresenter addPresenter;
    private ForumListAdapter adapter;
    private PageModel pageModel = new PageModel();


    @Override
    protected boolean interceptCreate(Bundle savedInstanceState) {
        userModel = (UserModel) getIntent().getSerializableExtra("model");
        return userModel == null;
    }

    @Override
    protected View getLayoutView() {
        customRecyclerView = new CustomRecyclerView(this);
        return customRecyclerView;
    }

    @Override
    protected int getLayoutId() {
        return 0;
    }

    @Override
    public void onBack() {
        finish();
    }

    @Override
    protected void initView() {
        super.initView();
        titleBar = customRecyclerView.getTitleBar();
        titleBar.setTitleStr(userModel.getNickName() + " 的帖子");
        isFriend = getIntent().getBooleanExtra("isFriend", false);
        if (!App.getApp().isVisitor() && !isFriend && !App.getApp().getUserId().equals(userModel.getUserId())){
            addPresenter = new OptFriendPresenter(new IViewAdapter<Boolean>() {
                @Override
                public void loadSuccess(BasePresenter presenter, BaseBean<Boolean> result) {
                    super.loadSuccess(presenter, result);
                    if (result.success && !result.data){
                        titleBar.setOptStr("加好友");
                        titleBar.setListener(PersonalActivity.this);
                    }
                }
            });
            addPresenter.work(userModel.getUserId());
        }

        forumListPresenter = new ForumListPresenter(this);
        managerPresenter(forumListPresenter);
        forumListPresenter.setUserInfo(userModel);
        adapter = new ForumListAdapter(customRecyclerView);
        customRecyclerView.setAdapter(adapter);
        customRecyclerView.setListener(this);
        onRefresh(null);
    }

    @Override
    public void onRefresh(RecyclerView recyclerView) {
        pageModel.reset();
        forumListPresenter.work(pageModel);
    }

    @Override
    public void loadMore(RecyclerView recyclerView) {
        if (pageModel.isHaveNextAndToast(this)){
            forumListPresenter.work(pageModel);
        }
    }

    @Override
    public void loadSuccess(BasePresenter presenter, BaseBean<List<ForumModel>> result) {
        if (result.success){
            if (pageModel.isFirstPage()){
                adapter.setList(result.data);
            }else{
                adapter.addList(result.data);
            }
            pageModel.setRequestCount(ListUtils.isNotNull(result.data) ? result.data.size() : 0);
        }else{
            ToastUtil.showShortToast(result.error);
            adapter.dataChange();
        }
    }

    @Override
    public void loadDone(BasePresenter presenter) {
        super.loadDone(presenter);
        customRecyclerView.stopRefreshing();
    }

    @Override
    public void loadFail(BasePresenter presenter, String error) {
        super.loadFail(presenter, error);
        customRecyclerView.stopRefreshing();
        ToastUtil.showShortToast(error);
    }

    @Override
    public boolean onClickBackView() {
        return false;
    }

    @Override
    public void onClickOptView() {
        addPresenter.addFriend(userModel.getUserId());
        titleBar.getOptView().setVisibility(View.GONE);
    }
}
